/**
 * jims
 */
package com.jims.master.modules.med.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.doctor.dao.ClinicOrdersCostDao;
import com.jims.master.modules.doctor.dao.ClinicOrdersDao;
import com.jims.master.modules.med.dao.MedicalCardConsumeMasterDao;
import com.jims.master.modules.med.dao.MedicalCardMasterDao;
import com.jims.master.modules.med.entity.MedicalCardConsumeMaster;
import com.jims.master.modules.med.entity.MedicalCardMaster;
import com.jims.master.modules.oper.entity.PriceList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 消费Service
 * @author 陈晓阳
 * @version 2017-05-22
 */
@Service
@Transactional(readOnly = true)
public class MedicalCardConsumeMasterService extends CrudService<MedicalCardConsumeMasterDao, MedicalCardConsumeMaster> {

    @Autowired
    private MedicalCardConsumeMasterDao medicalCardConsumeMasterDao;
    @Autowired
    private MedicalCardConsumeDetailService  medicalCardConsumeDetailService;
    @Autowired
    private MedicalCardMasterDao medicalCardMasterDao;
    @Autowired
    private ClinicOrdersDao clinicOrdersDao;
    @Autowired
    private ClinicOrdersCostDao clinicOrdersCostDao;

    public MedicalCardConsumeMaster get(String id) {
		return super.get(id);
	}
	
	public List<MedicalCardConsumeMaster> findList(MedicalCardConsumeMaster medicalCardConsumeMaster) {
		return super.findList(medicalCardConsumeMaster);
	}
	
	public Page<MedicalCardConsumeMaster> findPage(Page<MedicalCardConsumeMaster> page, MedicalCardConsumeMaster medicalCardConsumeMaster) {
		return super.findPage(page, medicalCardConsumeMaster);
	}

    /**
     * 保存消费信息
     * @param medicalCardConsumeMaster
     * @return
     */
    @Transactional(readOnly = false)
    public int save(MedicalCardConsumeMaster medicalCardConsumeMaster,List<PriceList> priceLists,String clinicNo) {
        int resultval=0;
        resultval+=super.save(medicalCardConsumeMaster);
        if(priceLists==null||priceLists.isEmpty()){
            priceLists=clinicOrdersCostDao.findpriceList(medicalCardConsumeMaster.getFeeId());
        }
        for(int i=0;i<priceLists.size();i++){
            medicalCardConsumeMaster.setData(priceLists.get(i));
            resultval+=medicalCardConsumeDetailService.save(medicalCardConsumeMaster.getMedicalCardConsumeDetail());
        }
        MedicalCardMaster medicalCardMaster=new MedicalCardMaster(medicalCardConsumeMaster.getCardNo());
        medicalCardMaster=medicalCardMasterDao.findByCardNo(medicalCardMaster);
//        medicalCardMaster.setClinicNo(clinicNo);
        medicalCardMaster.setBalance(medicalCardMaster.getBalance()-medicalCardConsumeMaster.getConsumeMoney());
        resultval+=medicalCardMasterDao.updateBalance(medicalCardMaster);
        return resultval;
    }
	
	@Transactional(readOnly = false)
	public int delete(MedicalCardConsumeMaster medicalCardConsumeMaster) {
		return super.delete(medicalCardConsumeMaster);
	}

    /**
     * 门诊收费 保存一卡通消费
     * @param baseDtos
     * @param cardNo
     * @param costs
     * @return
     */
    @Transactional(readOnly = false)
    public int outpSave(List<BaseDto> baseDtos, String cardNo, String costs) {
        int returnVal=0;
        for(BaseDto baseDto:baseDtos){
            String ordersIds= (String)baseDto.get("order_id");
             String [] ordersArry =ordersIds.split(",");
            if(ordersArry.length>0){
                List<String> list=Arrays.asList(ordersArry);
                List<BaseDto> baseDtoList=clinicOrdersDao.getInfo(list);
                for(BaseDto bd:baseDtoList){
                    MedicalCardConsumeMaster medicalCardConsumeMaster=new MedicalCardConsumeMaster();
                    medicalCardConsumeMaster.setConsumeMoney(Double.parseDouble((BigDecimal)bd.get("money")+""));//消费金额
                    medicalCardConsumeMaster.setClinicItemDictId("");
                    medicalCardConsumeMaster.setItemClass((String) bd.get("itemClass"));
                    medicalCardConsumeMaster.setRcptNo((String) bd.get("rcptNo"));
                    medicalCardConsumeMaster.setFeeId((String) bd.get("ordersId"));
                    medicalCardConsumeMaster.setCardNo(cardNo);
                    medicalCardConsumeMaster.setPatientId("");
                    medicalCardConsumeMaster.setFeeFlag("0");
                    returnVal+=save(medicalCardConsumeMaster,null,"");
                }
            }
         }
         return returnVal;
    }

    /**
     *
     * @param feeId
     * @return
     */
    public MedicalCardConsumeMaster getByfeeId(String feeId) {
        return medicalCardConsumeMasterDao.getByfeeId(feeId);
    }

    /**
     * 根据收据号查询消费记录
     * @param rcptLists
     * @return
     */
    public List<MedicalCardConsumeMaster> findByRcpts(List<String> rcptLists) {
        return medicalCardConsumeMasterDao.findByRcpts(rcptLists);
    }

    /**
     * 根据卡号查询rcptnos
     * @param cardNo
     * @return
     */
    public List<String> findByCardNo(String cardNo) {
        List<String> rcptNos=new ArrayList<String>();
        rcptNos= medicalCardConsumeMasterDao.findByCardNo(cardNo);
        return rcptNos;
    }
}